<?php
require_once APPPATH . "controllers/cmsbase.php";
class cms_settings extends CmsBase {

	var $_require_login = true;

	function __construct() {
		parent :: __construct();
		// Check permission
		parent::_check_permission('pms_setting');
		$this->load->model('Model_setting', 'model_setting');
	}

	/**
	 *
	 *
	 */
	function index() {
		$setting = $this->model_setting->first_row();
		return $this->_render_my_view(array ('setting' => $setting));
	}

	/**
	 *
	 *
	 */
	function add() {
		if ($this->input->post('Submit')) {
			$data = parse_input("item_", $_POST);
			$this->model_setting->insert($data);
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		return $this->_render_my_view(array());
	}

	/**
	 *
	 *
	 */
	function delete() {

	}

	/**
	 *
	 *
	 */
	function edit($alias) {
		$id = 1;
		if(!empty($alias)){
			if ($this->input->post('Submit')) {
				$data = parse_input("item_", $_POST);
				$this->model_setting->update_alias($data['name'],$data['value']);
			}
			else{
				$setting = $this->model_setting->first_row($id);
				$option = array('name' => splitUppercase($alias), 'value' => $setting[$alias]);

				return $this->_render_my_view(array ('option' => $option));
			}
		}
		redirect(site_url(array("c" => "cms_settings", "m" => "index")));
	}

	/**
	 *
	 *
	 */
	function edit_lang($id = 1){
		$setting = $this->model_setting->first_row();
		if ($this->input->post('Submit')) {
			$this->load->model('Model_country', 'model_country');
			$lang_current = $setting['language_support'];
			$lang_current = unserialize($lang_current);
			$lang_seperator = $this->config->item("lang_seperator");
			$model_multi_lang = $this->config->item("model_multi_lang");

			$data = $_POST['language_support'];
			foreach($data as $item){
				if(array_search($item, $lang_current)===FALSE){
					// Duplicate table
					foreach($model_multi_lang as $dup_model){
						$this->load->model($dup_model);
						// load iso_code
						$country = $this->model_country->find($item);
						$suffix = $lang_seperator.strtolower($country['iso_code_2']);
						$this->$dup_model->clone_table($suffix, true);
					}
				}
			}
			$data = serialize($data);
			$this->model_setting->update($id, array("language_support" => $data));
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		else{
			$this->load->model('Model_country', 'model_country');

			$countries = $this->model_country->fetch_all();
			return $this->_render_my_view(array ('setting' => $setting, 'countries' => $countries));
		}
	}

	/**
	 *
	 *
	 */
	function edit_date_format($id = 1){
		$setting = $this->model_setting->first_row();
		if ($this->input->post('Submit')) {
			$data = parse_input("item_",$_POST);

			$this->model_setting->update($id, $data);
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		else{
			return $this->_render_my_view(array ('setting' => $setting));
		}
	}
	/**
	 *
	 *
	 */
	function edit_image_toolkit($id = 1){
		$setting = $this->model_setting->first_row();
		if ($this->input->post('Submit')) {
			$data = parse_input("item_",$_POST);

			$this->model_setting->update($id, array("image_toolkit" => serialize($data)));
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		else{
			return $this->_render_my_view(array ('setting' => $setting));
		}
	}
	/**
	 *
	 *
	 */
	function edit_google_analytic($id = 1){
		$setting = $this->model_setting->first_row();
		if ($this->input->post('Submit')) {
			$data = parse_input("item_",$_POST);
			$data = serialize($data);

			$this->model_setting->update($id, array('google_analytic' => $data));
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		else{
			return $this->_render_my_view(array ('setting' => $setting));
		}
	}
	/**
	 *
	 *
	 */
	function edit_layout($id = 1){
		$setting = $this->model_setting->first_row();
		if ($this->input->post('Submit')) {
			$data = parse_input("item_",$_POST);
			$data = serialize($data);

			$this->model_setting->update($id, array('layout' => $data));
			redirect(site_url(array("c" => "cms_settings", "m" => "index")));
		}
		else{
			return $this->_render_my_view(array ('setting' => $setting));
		}
	}
}
?>
